package com.yf.controller;

import com.yf.bean.BaseResponse;
import com.yf.bean.MessageBean;
import com.yf.service.MessageService;
import com.yf.utils.UserContext;
import com.yf.utils.Utils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@RestController
@RequestMapping(value = "${custom.api-version}")
public class MessageController {
    @Autowired
    MessageService messageService;

    @GetMapping("/msg/num")
    public BaseResponse<Integer> getMsgNum() {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return BaseResponse.fail();
        }
        int msgNum = messageService.getMsgNum(uid);
        return BaseResponse.success(msgNum);
    }

    @PostMapping("/msg")
    public BaseResponse<Boolean> addMessage(@RequestBody MessageBean msg) {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return BaseResponse.fail();
        }
        msg.setCreated(Utils.currentTime());
        if (msg.getReceiver() == null) {
            msg.setReceiver(2);
        }
        msg.setSender(uid);
        if (msg.getSender().equals(msg.getReceiver())) {
            return BaseResponse.success(true);
        }
        boolean ret = messageService.addMessage(msg) > 0;
        return BaseResponse.success(ret);
    }

    @GetMapping("/msg")
    public BaseResponse<List<MessageBean>> getMsg(
            @RequestParam(name = "offset", defaultValue = "0") Integer offset,
            @RequestParam(name = "count", defaultValue = "20") Integer count) {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return BaseResponse.fail();
        }
        Map<String, Object> params = new HashMap<>(3);
        params.put("receiver", uid);
        params.put("offset", offset);
        params.put("count", count);
        List<MessageBean> msg = messageService.getMsgByUid(params);
        return BaseResponse.success(msg);
    }

    @GetMapping("/msg/num/unread")
    public BaseResponse<Integer> getMsgNumUnread() {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return BaseResponse.fail();
        }
        int msgNum = messageService.getMsgNumUnRead(uid);
        return BaseResponse.success(msgNum);
    }

    @PostMapping("/msg/read")
    public BaseResponse<Boolean> setMsgRead(
            @RequestParam(name = "id", defaultValue = "0") Integer id) {
        if (id == 0) {
            return BaseResponse.fail();
        }
        boolean r = messageService.setMsgStatus(id, 1);
        return BaseResponse.success(r);
    }

    @PostMapping("/msg/delete")
    public BaseResponse<Boolean> setMsgDelete(
            @RequestParam(name = "id", defaultValue = "0") Integer id) {
        if (id == 0) {
            return BaseResponse.fail();
        }
        boolean r = messageService.setMsgStatus(id, 2);
        return BaseResponse.success(r);
    }
}
